import htmlParser from '@/js_sdk/html-parser.js';
import emoji from '@/common/emoji.js';
import config from "./config.js";

 function parseImgs(nodes) {
	//console.log(nodes)
	nodes.forEach(node => {
	    if (
	      node.name === 'img' &&
	      node.attrs &&
	      node.attrs['data-img-size-val']
	    ) {
	      const sizes = node.attrs['data-img-size-val'].split(',')
	      const width =  sizes[0]
	      const height =  sizes[1]
	      node.attrs.style = `width:${width}px;height:${height}px;`
	    }
		
		if(node.type === 'text') {
		    //node.text = "哈哈哈"
			node.style = `line-height：30px;`
			node.class = "link"
		}
		
	    if(Array.isArray(node.children)) {
	      parseImgs(node.children)
	    }
		
	  })
	  
	  
	  
	  return nodes
}

function replaceEmoji(str) {
	if(!str)return;;
	let replacedStr = str.replace(/\[([^(\]|\[)]*)\]/g, (val, index) => {
			for (let i = 0; i < emoji.length; i++) {
				let EM = emoji[i];
				//console.log("emojiitem",EM)
				if (EM.alt == val) {
					return `<img data-img-size-val="18,18" style="panding:10px;width：20px;height:20px" src="${config.imgUrl + EM.url}"> &nbsp;`;
				}	
			}
			return val;
		});
		//console.log(replacedStr)
		return replacedStr;
	};		

//https://img.36krcdn.com/20200410/v2_921661f31f0f467fae90e634c93e042c_img_000
function parseText(content){
	//content = '<div style="vertical-align:middle;display: flex;align-items: center;flex-direction: row;padding-top:-4;">' + content + '</div>';
	content = replaceEmoji(content);
	content = htmlParser(content);
	content = parseImgs(content);
	return content
 }
export default parseText
